//
//  FSUserAddAttentionViewController.m
//  fanshu
//
//  Created by 许海峰 on 16/4/10.
//  Copyright © 2016年 FS. All rights reserved.
//

#import "FSUserAddAttentionViewController.h"
#import "FSUserAddAttentionCell.h"
#import "FSUserAddAttentionItem.h"
#import "AFNetworking.h"
#import "MJExtension.h"
#import "MJRefresh.h"
#import "SVProgressHUD.h"

@interface FSUserAddAttentionViewController ()
@property (nonatomic,strong) NSMutableArray *userItemArray;
/** 发送请求的管理者 */
@property (nonatomic, strong) AFHTTPSessionManager *manager;
@end
/** 请求路径 */
//NSString * XMGRequestURL = @"http://api.budejie.com/api/api_open.php";

@implementation FSUserAddAttentionViewController


- (AFHTTPSessionManager *)manager
{
    if (!_manager) {
        _manager = [AFHTTPSessionManager manager];
    }
    return _manager;
}

-(NSMutableArray *)userItemArray
{
    if (_userItemArray == nil) {
        _userItemArray = [NSMutableArray array];
    }
    return _userItemArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.tableView.contentInset = UIEdgeInsetsMake(99, 0, 0, 0);
    self.tableView.scrollIndicatorInsets = self.tableView.contentInset;
    //调整(iOS7以上)表格分隔线
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        self.tableView.separatorInset = UIEdgeInsetsZero;
    }
    //调整(iOS8以上)表格分隔线
    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        self.tableView.layoutMargins = UIEdgeInsetsZero;
    }
    // 马上进入刷新状态
    [self.tableView.mj_header beginRefreshing];
    [self loadWebData];
    [self setupTableViewHeaderFooter];
}
-(void)setupTableViewHeaderFooter
{
    UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 30, 40)];
    UILabel *commendLabel = [[UILabel alloc] init];
    commendLabel.text = @"简书推荐";
    commendLabel.x += 10;
    commendLabel.y = 18;
    commendLabel.font = [UIFont systemFontOfSize:12];
    [commendLabel sizeToFit];
    commendLabel.textColor = [UIColor grayColor];
    [header addSubview:commendLabel];
    
    UIView *lineH = [[UIView alloc] initWithFrame:CGRectMake(0, 39.5, ScreenWidth, 0.5)];
    lineH.backgroundColor = [[UIColor lightGrayColor] colorWithAlphaComponent:0.7];
    [header addSubview:lineH];
    
    self.tableView.tableHeaderView = header;
    
    UIView *footer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 30, 40)];
    UILabel *endLabel = [[UILabel alloc] init];
    endLabel.text = @"-- end --";
    [endLabel sizeToFit];
    endLabel.centerX = self.tableView.width * 0.5;
    
    endLabel.font = [UIFont systemFontOfSize:12];
    endLabel.textColor = [UIColor grayColor];
    [footer addSubview:endLabel];
    
    UIView *lineF = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 0.5)];
    lineF.backgroundColor = [[UIColor lightGrayColor] colorWithAlphaComponent:0.7];
    [footer addSubview:lineF];
    
    self.tableView.tableFooterView = footer;
}

-(void)loadWebData
{
    
    /*******************************/
    // 取消请求
    // 仅仅是取消请求, 不会关闭session
    [self.manager.tasks makeObjectsPerformSelector:@selector(cancel)];
    
    // 创建请求参数
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"a"] = @"list";
    parameters[@"c"] = @"data";
    parameters[@"type"] = @29;
    
    // 发送请求
    [self.manager GET:@"http://api.budejie.com/api/api_open.php" parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
        // 存储maxtime
//        self.maxtime = responseObject[@"info"][@"maxtime"];
        
        // 字典数组 -> 模型数组
        self.userItemArray = [FSUserAddAttentionItem mj_objectArrayWithKeyValuesArray:responseObject[@"list"]];
        
        // 刷新表格
        [self.tableView reloadData];
        
        // 结束刷新(恢复刷新控件的状态)
        [self.tableView.mj_header endRefreshing];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        // 结束刷新(恢复刷新控件的状态)
        [self.tableView.mj_header endRefreshing];

        
        // 如果是因为取消任务来到failure这个block, 就直接返回, 不需要提醒错误信息
        if (error.code == NSURLErrorCancelled) return;
        // 请求失败的提醒
        [SVProgressHUD showErrorWithStatus:@"网络繁忙,请稍后再试!"];
        
    }];

    /*************************/
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.userItemArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *ID = @"STCell";
    FSUserAddAttentionCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell = [[FSUserAddAttentionCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
    }
    cell.model = self.userItemArray[indexPath.row];
    //iOS8以后cell分隔线边距调整
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return [self.userItemArray[indexPath.row] cellHeight];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
